package com.web.kongjian.tag;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Comment;
import com.base.kongjian.entity.FooterPrint;
import com.base.kongjian.entity.Kongjian;
import com.base.kongjian.entity.Message;
import com.base.kongjian.entity.PopularitySystem;
import com.base.kongjian.entity.RealInform;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.entity.WealthSystem;
import com.base.kongjian.entity.Weibo;
import com.base.kongjian.entity.WorkShareRec;
import com.base.kongjian.entity.WorksNumCount;
import com.base.kongjian.service.CommentService;
import com.base.kongjian.service.FooterPrintService;
import com.base.kongjian.service.KongjianService;
import com.base.kongjian.service.MessageService;
import com.base.kongjian.service.PopularitySystemService;
import com.base.kongjian.service.RealInformService;
import com.base.kongjian.service.ReplyService;
import com.base.kongjian.service.WealthSystemService;
import com.base.kongjian.service.WeiboService;
import com.base.kongjian.service.WorkShareRecService;
import com.base.kongjian.service.WorksNumCountService;
import com.base.user.entity.BlackFriends;
import com.base.user.entity.Friends;
import com.base.user.entity.GZ;
import com.base.user.entity.Member;
import com.base.user.service.BlackFriendsService;
import com.base.user.service.FriendsService;
import com.base.user.service.GZService;
import com.base.user.service.MemberService;
import com.mysteel.core.SpringContext;
import com.mysteel.page.Page;
import com.mysteel.page.PageService;
import com.mysteel.util.DateUtils;
import com.web.kongjian.ao.utils.KongjianUtils;
import com.web.kongjian.dto.AllFriendsDTO;
import com.web.kongjian.dto.BasicInforDTO;
import com.web.kongjian.dto.BlackFriendsDTO;
import com.web.kongjian.dto.CollectionDTO;
import com.web.kongjian.dto.FooterPrintDTO;
import com.web.kongjian.dto.HeaderDTO;
import com.web.kongjian.dto.LeftDTO;
import com.web.kongjian.dto.MessageDTO;
import com.web.kongjian.dto.WeiboConditionDTO;
import com.web.user.member.utils.CookieUtils;

public class KongjianTag
{
	private WeiboService weiboService;
	private KongjianService kongjianService;
	private MemberService memberService;
	private MessageService messageService;
	private WealthSystemService wealthSystemService;
	private PopularitySystemService popularitySystemService;
	private WorksNumCountService worksNumCountService;
	private BlackFriendsService blackFriendsService;
	private FriendsService friendsService;
	private GZService gzService;
	private CommentService commentService;
	private ReplyService replyService;
	private PageService pageService;
	private WorkShareRecService workShareRecService;
	private FooterPrintService footerPrintService;
	private RealInformService realInformService;
	
	public KongjianTag ()
	{
		weiboService = (WeiboService) SpringContext.getBean("weiboService");
		kongjianService = (KongjianService) SpringContext.getBean("kongjianService");
		memberService = (MemberService) SpringContext.getBean("memberService");
		messageService = (MessageService) SpringContext.getBean("messageService");
		wealthSystemService = (WealthSystemService) SpringContext.getBean("wealthSystemService");
		popularitySystemService = (PopularitySystemService) SpringContext.getBean("popularitySystemService");
		worksNumCountService = (WorksNumCountService) SpringContext.getBean("worksNumCountService");
		blackFriendsService = (BlackFriendsService) SpringContext.getBean("blackFriendsService");
		gzService = (GZService) SpringContext.getBean("gzService");
		friendsService = (FriendsService) SpringContext.getBean("friendsService");
		commentService = (CommentService) SpringContext.getBean("commentService");
		replyService = (ReplyService) SpringContext.getBean("replyService");
		pageService = (PageService) SpringContext.getBean("pageService");
		workShareRecService = (WorkShareRecService) SpringContext.getBean("workShareRecService");
		footerPrintService = (FooterPrintService) SpringContext.getBean("footerPrintService");
		realInformService = (RealInformService) SpringContext.getBean("realInformService");
	}
	
	/**
	 * ???
	 * @param request
	 * @param memberId
	 * @return
	 */
	public HeaderDTO headerTag(HttpServletRequest request)
	{
		
		long id = KongjianUtils.getMemberIdByCookie (request);
		
		HeaderDTO headerDto  = null;
		
		
		Member m = memberService.getMemberById (id);
		
		if(ObjectUtils.notNull (m))
		{
			headerDto  = new HeaderDTO ();
			headerDto.setNickName (m.getNickName ());
			headerDto.setMemberId (id);
		
		
		Kongjian kongjian = kongjianService. getKJByMemberId (id);
		
		if(ObjectUtils.notNull (kongjian))
		{
			headerDto.setPraise_num (kongjian.getPraiseCount ());
		}
		
		long unReaderM = messageService.getUnReadCount (id);
		
		headerDto.setMessage_num (unReaderM);
		}
		return headerDto;
	}
	
	
	/**
	 * ??????????
	 * @param request
	 * @return
	 */
	public BasicInforDTO basicInforTag(HttpServletRequest request)
	{
		BasicInforDTO bid =  new BasicInforDTO ();
		
		long memberId = Long.parseLong (request.getParameter ("memberId"));
		
		Member member = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			bid.setNickName (member.getNickName ());
			bid.setHeaderUrl (member.getHeadUrl ());
		}
		
		Kongjian kj = kongjianService.getKJByMemberId (memberId) ;
		
		if(ObjectUtils.notNull (kj))
		{
			bid.setLevel (kj.getLevel ());
		}
		
		WealthSystem ws =  wealthSystemService.getWSByMemberId (memberId);
		
		if(ObjectUtils.notNull (ws))
		{
			bid.setDiamond_num (ws.getDiamondNum ());
			bid.setFans_num (ws.getFansNum ());
			bid.setFlower_num (ws.getFlowerNum ());
			bid.setLoverStar_num (ws.getLoverStarNum ());
		}
		
		PopularitySystem ps = popularitySystemService.getByMemberId (memberId);
		
		if(ObjectUtils.notNull (ps))
		{
			bid.setPopularity_num (ps.getValue ());
		}
		
		WorksNumCount wnc = worksNumCountService.getItsByMemberIed (memberId);
		
		if(ObjectUtils.notNull (wnc))
		{
			bid.setWeibo_num (wnc.getWeiboNum ());
			bid.setPhoto_num (wnc.getAlbumNum ());
			bid.setMusic_num (wnc.getMusicNum ());
		}
		
		return bid;
	}
	
	/**
	 * ???
	 * @param request
	 * @return
	 */
	public LeftDTO leftTag(HttpServletRequest request)
	{
		LeftDTO leftDto = new LeftDTO();
		
		long memberId = Long.parseLong (request.getParameter ("memberId"));
		
		long visitorNum = footerPrintService.getAllVisitorNum (memberId);
		
		leftDto.setVisitor_num (visitorNum);
		
		long all_Mess_Num = messageService.getReadCount (memberId)+messageService.getUnReadCount (memberId);
		
		leftDto.setAll_Mess_num (all_Mess_Num);
		leftDto.setUnread_Mess_num (messageService.getUnReadCount (memberId));
		leftDto.setAllWork_num (worksNumCountService.getAllWorkNum (memberId));
		leftDto.setBlackList_num (blackFriendsService.getBlackNum (memberId));
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("sharerId" ,memberId);
		params.put ("type" ,2);
		Page<WorkShareRec> page = workShareRecService.queryWSR (params ,0 ,-1);
		if(ObjectUtils.notNull (page))
		{
			List<WorkShareRec> list = page.getDatas ();
			
			if(ObjectUtils.notEmpty (list))
			{
				leftDto.setCollection_num (list.size ());//??????????
			}
		}
		params.put ("memberId" ,memberId);
		
		Page<Friends> pageFriend = friendsService.query (params ,0 ,-1);
		
		long fanNum = 0L;
		if(ObjectUtils.notNull (pageFriend))
		{
			List<Friends> fs = pageFriend.getDatas ();
			
			if(ObjectUtils.notEmpty (fs))
			{
				fanNum = fs.size ();
			}
		}
		leftDto.setFans_num (fanNum);//??ú?????
		WealthSystem ws = wealthSystemService.getWSByMemberId (memberId);
		if(ObjectUtils.notNull (ws))
		{
			ws.setFansNum (fanNum);
			wealthSystemService.updateWS (ws);
		}
		
		return leftDto;
	}
	
	public List<Member> kjSortTag()
	{
		List<Member> list = new ArrayList<Member>();
		
		
		Page<PopularitySystem> page = popularitySystemService.queryPopularitySystem (null ,1 ,9);
		
		if(ObjectUtils.notNull (page))
		{
			
			for(PopularitySystem ps :page.getDatas ())
			{
				list.add (memberService.getMemberById (ps.getMemberId ()));
			}
			
		}
		
		return list;
	}
	
	/**
	 * ??????weibo
	 * @param request
	 * @return
	 */
	public Page<WeiboConditionDTO> getFriendsWeiboCondition(HttpServletRequest request,int MaxPageRowCount)
	{
		List<WeiboConditionDTO> listConditionDTOs = new ArrayList<WeiboConditionDTO> ();
		
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		
		String pageString = request.getParameter ("current_page");
		long current_page = 1;
		if(!StringUtils.isTrimEmpty (pageString))
		{
			current_page = Long.parseLong (pageString);
		}
		
		List<Friends> friendList = friendsService.getAllFriendByMemberId (memberId); //??????е?????
		List<GZ> gzs = gzService.getAllGZByMemberId (memberId);						//??????е???,???????????????????????????????????????
		List<Member> listMembers = new ArrayList<Member> ();
		
		if(ObjectUtils.notNull (friendList)&&ObjectUtils.notEmpty (friendList))
		{
			for(Friends friends : friendList)
			{
				Member member = memberService.getMemberById (friends.getFriend_id ());
				if(ObjectUtils.notNull (member))
				{
					listMembers.add (member);
				}
			}
		}
		if(ObjectUtils.notNull (gzs)&&ObjectUtils.notEmpty (gzs))
		{
			for(GZ gz : gzs)
			{
				Member member = memberService.getMemberById (gz.getFriend_id ());
				if(ObjectUtils.notNull (member))
				{
					listMembers.add (member);
				}
			}
		}
		
		listMembers = removeSaveMember(listMembers);
		
		List<Weibo> weibos = new ArrayList<Weibo>();
		Map<String, Object> params = new HashMap<String,Object> ();
		
		for(Member member : listMembers)
		{
			
			if(ObjectUtils.notNull (member))
			{
				params.put ("memberId" ,member.getId ());
			}
			
			Page<Weibo> p = weiboService.queryWeibo (params ,0 ,-1);
			
			if(ObjectUtils.notNull (p))
			{
				for(Weibo weibo : p.getDatas ())
				{
					weibos.add (weibo); //????????е????????
				}
			}
		}
		weibos = weiboSortByPubsherTime (weibos);
		
		long size = weibos.size ();
		long remainder = size%MaxPageRowCount;
		long TotalRowCount = 0L;
		
		if(remainder!=0)
		{
			TotalRowCount = (size/MaxPageRowCount)+1;
		}else
		{
			TotalRowCount = size/MaxPageRowCount;
		}
		
		List<Weibo> newWeibos = new ArrayList<Weibo>();
		
		if(current_page==TotalRowCount&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)current_page*MaxPageRowCount-2;i<size;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}else if(current_page==1&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = 0;i<MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		else if(ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)(current_page*MaxPageRowCount-2);i<(current_page*MaxPageRowCount-2)+MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		if(ObjectUtils.notEmpty (newWeibos))
		{
			for(Weibo w:newWeibos)
			{
				WeiboConditionDTO wct = new WeiboConditionDTO ();
				wct.setMember (memberService.getMemberById (w.getMemberId ()));
				wct.setWeibo (w);
				Map<String, Object> paramsComMap = new HashMap<String,Object>();
				paramsComMap.put ("workId" ,w.getId ());
				paramsComMap.put ("workKind" ,1);
				Page<Comment> pageCommPage = commentService.queryComment (paramsComMap ,0 ,-1);
				List<Comment> comments = new ArrayList<Comment>();
				
				if(ObjectUtils.notNull (pageCommPage))
				{
					comments = pageCommPage.getDatas ();
					wct.setComments (comments);
				}
				
				if(ObjectUtils.notEmpty (comments))
				{
					Map<Long, List<Reply>> replyMap = new HashMap<Long,List<Reply>> ();
					for(Comment c : comments)
					{
						Map<String, Object> paramsreplyMap = new HashMap<String,Object>();
						
						paramsreplyMap.put ("commentId" ,c.getId ());
						
						Page<Reply> pageReply = replyService.queryReply (paramsreplyMap ,0 ,-1);
						
						if(ObjectUtils.notNull (pageReply))
						{
							replyMap.put ((Long)c.getId () ,pageReply.getDatas ());
						}
					}
					
					wct.setReMap (replyMap);
				}
				
				listConditionDTOs.add (wct);
			}
		}
		
		Page<WeiboConditionDTO> pageDTO = new Page<> ();
		
		pageDTO = new Page<WeiboConditionDTO> ();
		pageDTO.setDatas (listConditionDTOs);
		pageDTO.setTotalRowCount ((int)size);
		pageDTO.setCurrentPageNum ((int)current_page);
		pageDTO.setMaxPageRowCount (MaxPageRowCount);
		pageDTO.setURL ("/u/"+memberId+"?&current_page=${pageNum}");
		pageDTO.setTemplate ("pshow");
		pageService.build (pageDTO);
		
		return pageDTO;
	}
	
	
	public List<Weibo> weiboSortByPubsherTime(List<Weibo> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getPublishTime ()<list.get (j).getPublishTime ())
				{
					Weibo temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	public List<WorkShareRec> workShareRecSortByPubsherTime(List<WorkShareRec> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getShareTime ()<list.get (j).getShareTime  ())
				{
					WorkShareRec temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * ???
	 * @param list
	 * @return
	 */
	public List<Member> removeSaveMember(List<Member> list)
	{
		long length = list.size ();
		
		for(int i = 0;i<length;i++)
		{
			for(int j = i+1;j<list.size();j++)
			{
				if(ObjectUtils.notNull (list.get (i))&&ObjectUtils.notNull (list.get (j)))
				{
					if(list.get (i).getId ()==list.get (j).getId ())
					{
						list.remove (j);
					}
				}
			}
			
		}
		
	/*	for(Member m :list)
		{
			/System.out.println (list.size ()+"---id"+m.getId ());
		}*/
		
		return list;
	}
	
	public static String timeTag(long time)
	{
		long currentTime = System.currentTimeMillis ();
		
		String currentTimeStr = DateUtils.formatDate (currentTime ,"yyyy-MM-dd-HH-mm");
		String hisTimeStr = DateUtils.formatDate (time ,"yyyy-MM-dd-HH-mm");
		
		String[] currentTimeArr = currentTimeStr.split ("-");
		String[] hisTimeArr = hisTimeStr.split ("-");
		
		for(String str:currentTimeArr)
		{
			System.out.println (str);
		}
		
		if(Long.parseLong (currentTimeArr[0])-Long.parseLong (hisTimeArr[0])>0L)
		{
			long year = Long.parseLong (currentTimeArr[0])-Long.parseLong (hisTimeArr[0]);
			
			return year+"???";
		}
		if(Long.parseLong (currentTimeArr[1])-Long.parseLong (hisTimeArr[1])>0L)
		{
			long month = Long.parseLong (currentTimeArr[1])-Long.parseLong (hisTimeArr[1]);
			
			return month+"???";
		}
		
		if(Long.parseLong (currentTimeArr[2])-Long.parseLong (hisTimeArr[2])>0L)
		{
			long month = Long.parseLong (currentTimeArr[2])-Long.parseLong (hisTimeArr[2]);
			
			if(month==1)
			{
				return "????"+hisTimeArr[3]+":"+hisTimeArr[4];
			}
			if(month==2)
			{
				return "???"+hisTimeArr[3]+":"+hisTimeArr[4];
			}
			
			return month+"???";
		}
		
		if(Long.parseLong (currentTimeArr[3])-Long.parseLong (hisTimeArr[3])>0L)
		{
			//long day = Long.parseLong (currentTimeArr[3])-Long.parseLong (hisTimeArr[3]);
			
			return "????"+hisTimeArr[3]+":"+hisTimeArr[4];
		}
		
		if(Long.parseLong (currentTimeArr[4])-Long.parseLong (hisTimeArr[4])>0L)
		{
			long day = Long.parseLong (currentTimeArr[4])-Long.parseLong (hisTimeArr[4]);
			
			return day+"?????";
		}
		
		return "???";
	}
	

	
	public Map<String, Object> judge(long workId,HttpServletRequest request)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		
		long memberId = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("workId" ,workId);
		params.put ("sharerId" ,memberId);
		params.put ("type" ,1);//1-??? ,2-???,3-????,4????
		params.put ("workKind" ,1);//???????1-???,2-???,3-????,4-????,5-???
		WorkShareRec wsc = workShareRecService.getWSRByMap (params);
		if (ObjectUtils.notNull (wsc))
		{
			returnMap.put ("transmit" ,"yes");
		}else
		{
			returnMap.put ("transmit" ,"no");
		}
		params.put ("type" ,2);
		
		wsc = workShareRecService.getWSRByMap (params);
		if (ObjectUtils.notNull (wsc))
		{
			returnMap.put ("collection" ,"yes");
		}else
		{
			returnMap.put ("collection" ,"no");
		}
		params.put ("type" ,3);
		
		wsc = workShareRecService.getWSRByMap (params);
		if (ObjectUtils.notNull (wsc))
		{
			returnMap.put ("praise" ,"yes");
		}else
		{
			returnMap.put ("praise" ,"no");
		}
		
		return returnMap;
	}
	

	/**
	 * ???
	 * @param request
	 * @return
	 */
	public Page<MessageDTO> messageTag(HttpServletRequest request)
	{
		
		String str = CookieUtils.getCookieValue(request, "member_id");
		Page<MessageDTO> returnPage = new Page<MessageDTO>();
		
		long memberId = 0L;
		
		if(!StringUtils.isTrimEmpty(str))
		{
			memberId = Long.parseLong(str);
		}
		
		String messageFlag = request.getParameter("messType");
		int current_page = 1;
		String pageStr = request.getParameter("current_page");
		int classFlag = Integer.parseInt(request.getParameter("classFlag"));
		if(!StringUtils.isTrimEmpty(pageStr))
		{
			current_page = Integer.parseInt(pageStr);
		}
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("memberId", memberId);
		String url = "/u/"+memberId+"?conPage=kj_Mess&classFlag="+classFlag;
		
		if(messageFlag.equals("all"))//??????
		{
		}
		if(messageFlag.equals("hasRead"))
		{
			params.put("status", 2);//???
		}
		url = url+"&messType="+messageFlag;
		
		if(messageFlag.equals("unRead"))
		{
			params.put("status", 1);
		}
		
		Page<Message> messPage = messageService.queryMessage(params, current_page, 9);
		
		List<MessageDTO> listDTO = null;
		if(ObjectUtils.notNull(messPage))
		{
			
			List<Message> list = messPage.getDatas();
			
			if(ObjectUtils.notEmpty(list))
			{
				listDTO = new ArrayList<MessageDTO>();
				for(Message m:list)
				{
					MessageDTO messDTO = new MessageDTO();
					messDTO.setMessageContent(m.getContent());
					int type = m.getType();
					//1-????,2-??????????3-???????4-?????
					if(type==1)
					{
						messDTO.setMessageType("???????");
					}
					if(type==2)
					{
						messDTO.setMessageType("????????");
					}
					if(type==3)
					{
						messDTO.setMessageType("??????");
					}
					if(type==4)
					{
						messDTO.setMessageType("?????");
					}
					
					messDTO.setSendM_Id(m.getSend_MId());
					messDTO.setMessageId(m.getId());
					Member member = memberService.getMemberById(m.getSend_MId());
					if(ObjectUtils.notNull(member))
					{
						messDTO.setSendM_nickName(member.getNickName());
					}
					
					messDTO.setSendTime(timeTag(m.getCreateTime()));
					
					listDTO.add(messDTO);
				}
				
				
			}
		
		
		returnPage.setDatas (listDTO);
		returnPage.setTotalRowCount (messPage.getTotalRowCount());
		returnPage.setCurrentPageNum (current_page);
		returnPage.setMaxPageRowCount (messPage.getMaxPageRowCount());
		returnPage.setURL (url+"?&current_page=${pageNum}");
		returnPage.setTemplate ("pshow");
		pageService.build (returnPage);
		}
		return returnPage;
	}
	
	/**
	 * ??????
	 * @param request
	 * @return
	 */
	public Map<String,Object> messageDetailTag(HttpServletRequest request)
	{
		Map<String,Object> returnMap = new HashMap<String,Object>();
		
		String messageTypeStr = request.getParameter("messageType");
		String messageContentStr = request.getParameter("messageContent");
		String sendM_nickNameStr = request.getParameter("sendM_nickName");
		String sendTimeStr = request.getParameter("sendTime");
		String messageIdStr = request.getParameter("messageId");
		long messageId = 0L;
		try 
		{
			if(!StringUtils.isTrimEmpty(messageTypeStr))
			{
				String messageType;
				messageType = new String(messageTypeStr.getBytes("iso8859-1"),"gbk");
				returnMap.put("messageType", messageType);
			}
			if(!StringUtils.isTrimEmpty(messageContentStr))
			{
				String messageContent;
				messageContent = new String(messageContentStr.getBytes("iso8859-1"),"gbk");
				returnMap.put("messageContent", messageContent);
			}
			if(!StringUtils.isTrimEmpty(sendM_nickNameStr))
			{
				String sendM_nickName;
				sendM_nickName = new String(sendM_nickNameStr.getBytes("iso8859-1"),"gbk");
				returnMap.put("sendM_nickName", sendM_nickName);
			}
			if(!StringUtils.isTrimEmpty(sendTimeStr))
			{
				String sendTime;
				sendTime = new String(sendTimeStr.getBytes("iso8859-1"),"gbk");
				returnMap.put("sendTime", sendTime);
			}
			
			if(!StringUtils.isTrimEmpty(messageIdStr))
			{
				
				messageId = Long.parseLong(messageIdStr);
				returnMap.put("messageId", messageId);
				
				Message m = messageService.getMessageById(messageId);
				
				if(ObjectUtils.notNull(m))
				{
					returnMap.put("send_MId", m.getSend_MId());
					returnMap.put("memberId", m.getMemberId());
				}
				
				m.setStatus(2);
				messageService.updateMessage(m);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return returnMap;
	}
	
	/**
	 * ???????????
	 * @param memberId
	 * @param send_MId
	 * @return
	 */
	public boolean isFriend(long memberId,long send_MId)
	{
		boolean b  = friendsService.isFriend(memberId, send_MId);
		
		return b;
	}
	
	/**
	 * ???????б?
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<CollectionDTO> collectionTag(HttpServletRequest request,int maxPageRowCount)
	{
		String str = CookieUtils.getCookieValue (request ,"member_id");
		long memberId = 0L;
		if(!StringUtils.isTrimEmpty (str))
		{
			memberId = Long.parseLong (str);
		}
		
		str = request.getParameter ("current_page");
		int currentPageNum = 1;
		if(!StringUtils.isTrimEmpty (str))
		{
			currentPageNum = Integer.parseInt (str);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		
		params.put ("sharerId" ,memberId);
		params.put ("type" ,2);
		Page<WorkShareRec> page = workShareRecService.queryWSR (params ,currentPageNum ,maxPageRowCount);
		List<CollectionDTO> dtos = null;
		if(ObjectUtils.notNull (page))
		{
			dtos = new ArrayList<CollectionDTO> ();
			
			List<WorkShareRec> list = page.getDatas ();
			list = workShareRecSortByPubsherTime(list);
			
			for(WorkShareRec wrc:list)
			{
				int workKind = wrc.getWorkKind ();
				//???????1-???,2-???,3-????,4-????,5-???	
				CollectionDTO  collectionDTO = new CollectionDTO ();
				if(workKind == 1)
				{
					Weibo w = weiboService.getById (wrc.getWorkId ());
					
					if(ObjectUtils.notNull (w))
					{
						collectionDTO.setTitle (w.getTitle ());//title
						collectionDTO.setId (w.getId ());
						collectionDTO.setType ("???");
						collectionDTO.setSendM_Id (wrc.getPublisherId ());
						collectionDTO.setTime (timeTag (wrc.getShareTime ()));
						Member member = memberService.getMemberById (wrc.getPublisherId  ());
						if(ObjectUtils.notNull (member))
						{
							collectionDTO.setSendM_NickName (member.getNickName ());
						}
							dtos.add (collectionDTO);
						}
					}
			}
		}
		
		Page<CollectionDTO> pageDTO = new Page<CollectionDTO> ();
		pageDTO.setDatas (dtos);
		pageDTO.setTotalRowCount (dtos.size ());
		pageDTO.setCurrentPageNum (currentPageNum);
		pageDTO.setMaxPageRowCount (page.getMaxPageRowCount ());
		pageDTO.setURL ("/u/"+memberId+"?&current_page=${pageNum}");
		pageDTO.setTemplate ("pshow");
		pageService.build (pageDTO);
		
		return pageDTO;
	}
	
	/**
	 * ??????
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public Map<String, Object> collectionDetailTag(HttpServletRequest request) throws UnsupportedEncodingException
	{
		String sendM_NickName = "";
		String collectionTime = "";
		String type = "";
		
		Map<String, Object> returnMap = new HashMap<String,Object>();
		String idStr = request.getParameter ("workId");
		String sendM_NickNameStr = request.getParameter ("sendM_NickName");
		String collectionTimeStr = request.getParameter ("time");
	
		String typeStr = request.getParameter ("type");
		
		if(!StringUtils.isTrimEmpty (sendM_NickNameStr))
		{
			sendM_NickName = new String(sendM_NickNameStr.getBytes("iso8859-1"),"gbk");
			returnMap.put ("sendM_NickName" ,sendM_NickName);
		}
		
		if(!StringUtils.isTrimEmpty (collectionTimeStr))
		{
			collectionTime = new String(collectionTimeStr.getBytes("iso8859-1"),"gbk");
			returnMap.put ("collectionTime" ,collectionTime);
		}
		
		if(!StringUtils.isTrimEmpty (typeStr))
		{
			try
			{
				type = new String(typeStr.getBytes("iso8859-1"),"gbk");
				if(type.equals ("???"))
				{
					long workId = Long.parseLong (idStr);
					Weibo weibo = weiboService.getById (workId);
					if(ObjectUtils.notNull (weibo))
					{
						returnMap.put ("type" ,"???");
						returnMap.put ("title" ,weibo.getTitle ());
						returnMap.put ("content" ,weibo.getContent ());
						returnMap.put ("collectionCount" ,weibo.getCollectionCount ());
						returnMap.put ("commentCount" ,weibo.getCommentCount ());
						returnMap.put ("browseCount" ,weibo.getBrowseCount ());
						returnMap.put ("transmitCount" ,weibo.getTransmitCount ());
						returnMap.put ("praiseCount" ,weibo.getPraiseCount ());
						returnMap.put ("publisherTime" ,timeTag (weibo.getPublishTime ()));
						returnMap.put ("workId" ,weibo.getId ());
					}
				}
			}catch (UnsupportedEncodingException e)
			{
				e.printStackTrace();
			}
		}
		
		return returnMap;
	}
	
	public List<FooterPrint> footerPrintSortByLastVisitorTime(List<FooterPrint> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getLastVisitTime  ()<list.get (j).getLastVisitTime  ())
				{
					FooterPrint temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	public static List<Friends> friendsSortByCreateTime(List<Friends> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getCreateTime   ()<list.get (j).getCreateTime  ())
				{
					Friends temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * 
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<FooterPrintDTO> footerPrintTag(HttpServletRequest request,int maxPageRowCount)
	{
		Page<FooterPrintDTO> returnPage = null;
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long memberId = 0L;
		
		if(!StringUtils.isTrimEmpty (str))
		{
			memberId = Long.parseLong (str);
		}
		
		int currentPageNum = 1;
		String current_pageStr = request.getParameter ("current_page");
		
		if(!StringUtils.isTrimEmpty (current_pageStr))
		{
			currentPageNum = Integer.parseInt (current_pageStr);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		
		Page<FooterPrint> page = footerPrintService.queryFP (params ,currentPageNum ,maxPageRowCount);
		List<FooterPrintDTO> dtos = null;
		if(ObjectUtils.notNull (page))
		{
			returnPage = new Page<FooterPrintDTO> ();
			dtos = new ArrayList<FooterPrintDTO>();
			List<FooterPrint> list = page.getDatas ();
			list = footerPrintSortByLastVisitorTime (list);
			for(FooterPrint fp:list)
			{
				long visitor_MId = fp.getVisitor_MId ();
				Member m = memberService.getMemberById (visitor_MId);
				if(ObjectUtils.notNull (m))
				{
					FooterPrintDTO fpdto = new FooterPrintDTO ();
					fpdto.setId (m.getId ());
					String headUrl = m.getHeadUrl ();
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
					}
					fpdto.setHeadUrl (headUrl);
					fpdto.setNickName (m.getNickName ());
					fpdto.setVisitorTime (timeTag (fp.getLastVisitTime ()));
					fpdto.setAllVisitorNum (fp.getVisitorNum ());
					
					dtos.add (fpdto);
				}
			}
			returnPage.setDatas (dtos);
			returnPage.setTotalRowCount (dtos.size ());
			returnPage.setCurrentPageNum (currentPageNum);
			returnPage.setMaxPageRowCount (page.getMaxPageRowCount ());
			returnPage.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=footerPrint");
			returnPage.setTemplate ("pshow");
			pageService.build (returnPage);
		}
		
		return returnPage;
	}
	
	/**
	 * ????????????
	 * @param request
	 * @param MaxPageRowCount
	 * @return
	 */
	public Page<WeiboConditionDTO> allWorksTag(HttpServletRequest request,int MaxPageRowCount)
	{
		List<WeiboConditionDTO> listConditionDTOs = new ArrayList<WeiboConditionDTO> ();
		List<Member> listMembers = new ArrayList<Member>();
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			listMembers.add (m);
		}
		
		List<Weibo> weibos = new ArrayList<Weibo>();
		Map<String, Object> params = new HashMap<String,Object> ();
		
		for(Member member : listMembers)
		{
			
			if(ObjectUtils.notNull (member))
			{
				params.put ("memberId" ,member.getId ());
			}
			
			Page<Weibo> p = weiboService.queryWeibo (params ,0 ,-1);
			
			if(ObjectUtils.notNull (p))
			{
				for(Weibo weibo : p.getDatas ())
				{
					weibos.add (weibo); //????????е????????
				}
			}
		}
		weibos = weiboSortByPubsherTime (weibos);
		
		long size = weibos.size ();
		long remainder = size%MaxPageRowCount;
		long TotalRowCount = 0L;
		
		if(remainder!=0)
		{
			TotalRowCount = (size/MaxPageRowCount)+1;
		}else
		{
			TotalRowCount = size/MaxPageRowCount;
		}
		String pageString = request.getParameter ("current_page");
		long current_page = 1;
		if(!StringUtils.isTrimEmpty (pageString))
		{
			current_page = Long.parseLong (pageString);
		}
		
		List<Weibo> newWeibos = new ArrayList<Weibo>();
		
		if(current_page==TotalRowCount&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)current_page*MaxPageRowCount-2;i<size;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}else if(current_page==1&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = 0;i<MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		else if(ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)(current_page*MaxPageRowCount-2);i<(current_page*MaxPageRowCount-2)+MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		if(ObjectUtils.notEmpty (newWeibos))
		{
			for(Weibo w:newWeibos)
			{
				WeiboConditionDTO wct = new WeiboConditionDTO ();
				wct.setMember (memberService.getMemberById (w.getMemberId ()));
				wct.setWeibo (w);
				Map<String, Object> paramsComMap = new HashMap<String,Object>();
				paramsComMap.put ("workId" ,w.getId ());
				paramsComMap.put ("workKind" ,1);
				Page<Comment> pageCommPage = commentService.queryComment (paramsComMap ,0 ,-1);
				List<Comment> comments = new ArrayList<Comment>();
				
				if(ObjectUtils.notNull (pageCommPage))
				{
					comments = pageCommPage.getDatas ();
					wct.setComments (comments);
				}
				
				if(ObjectUtils.notEmpty (comments))
				{
					Map<Long, List<Reply>> replyMap = new HashMap<Long,List<Reply>> ();
					for(Comment c : comments)
					{
						Map<String, Object> paramsreplyMap = new HashMap<String,Object>();
						
						paramsreplyMap.put ("commentId" ,c.getId ());
						
						Page<Reply> pageReply = replyService.queryReply (paramsreplyMap ,0 ,-1);
						
						if(ObjectUtils.notNull (pageReply))
						{
							replyMap.put ((Long)c.getId () ,pageReply.getDatas ());
						}
					}
					
					wct.setReMap (replyMap);
				}
				
				listConditionDTOs.add (wct);
			}
		}
		
		Page<WeiboConditionDTO> pageDTO = new Page<> ();
		
		pageDTO = new Page<WeiboConditionDTO> ();
		pageDTO.setDatas (listConditionDTOs);
		pageDTO.setTotalRowCount ((int)size);
		pageDTO.setCurrentPageNum ((int)current_page);
		pageDTO.setMaxPageRowCount (MaxPageRowCount);
		pageDTO.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=allWorks");
		pageDTO.setTemplate ("pshow");
		pageService.build (pageDTO);
		
		return pageDTO;
	}
	
	/**
	 * ?????б?
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<AllFriendsDTO> AllFriendsTag(HttpServletRequest request,int maxPageRowCount)
	{
		Page<AllFriendsDTO> returnPage = null;
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long memberId = 0L;
		
		if(!StringUtils.isTrimEmpty (str))
		{
			memberId = Long.parseLong (str);
		}
		
		int currentPageNum = 1;
		String current_pageStr = request.getParameter ("current_page");
		
		if(!StringUtils.isTrimEmpty (current_pageStr))
		{
			currentPageNum = Integer.parseInt (current_pageStr);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		
		Page<Friends> page = friendsService. query (params ,currentPageNum ,maxPageRowCount);
		List<AllFriendsDTO> dtos = null;
		if(ObjectUtils.notNull (page))
		{
			returnPage = new Page<AllFriendsDTO> ();
			dtos = new ArrayList<AllFriendsDTO>();
			List<Friends> list = page.getDatas ();
			list = friendsSortByCreateTime (list);
			for(Friends fp:list)
			{
				long visitor_MId = fp.getFriend_id  ();
				Member m = memberService.getMemberById (visitor_MId);
				if(ObjectUtils.notNull (m))
				{
					AllFriendsDTO fpdto = new AllFriendsDTO ();
					fpdto.setId (m.getId ());
					String headUrl = m.getHeadUrl ();
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
					}
					
					fpdto.setHeadUrl (headUrl);
					fpdto.setNickName (m.getNickName ());
					fpdto.setVisitorTime (timeTag (fp.getCreateTime  ()));
					
					dtos.add (fpdto);
				}
			}
			returnPage.setDatas (dtos);
			returnPage.setTotalRowCount (dtos.size ());
			returnPage.setCurrentPageNum (currentPageNum);
			returnPage.setMaxPageRowCount (page.getMaxPageRowCount ());
			returnPage.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=allFriends");
			returnPage.setTemplate ("pshow");
			pageService.build (returnPage);
		}
		
		return returnPage;
	}
	
	/**
	 * ??????????
	 * @param request
	 * @return
	 */
	public Map<String, Object> userInforTag(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Map<String, Object> returnMap = new HashMap<String,Object>();
		Member member = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			returnMap.put ("member" ,member);
		}
		
		return returnMap;
	}
	
	public String editHeaderTag(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Member member = memberService.getMemberById (memberId);
		
		String headerUrl = member.getHeadUrl ();
		if(StringUtils.isTrimEmpty (headerUrl))
		{
			headerUrl = "/skin/images/kongjian/noavatar_small.gif";
		}
		
		return headerUrl;
	}
	
	public String editSignTag(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		Member member = memberService.getMemberById (memberId);
		String str = "";
		if(ObjectUtils.notNull (member))
		{
			str = member.getSign ();
		}
		
		return str;
	}
	
	/**
	 * ?????????
	 * @param request
	 * @return
	 */
	public RealInform realInformTag(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		RealInform realInform = realInformService.getRIByMemberId (memberId);
		
		return realInform;
	}
	
	
	/**
	 * ???????б?
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<BlackFriendsDTO> blackFriendsTag(HttpServletRequest request,int maxPageRowCount)
	{
		Page<BlackFriendsDTO> returnPage = null;
		String str = CookieUtils.getCookieValue (request ,"member_id");
		
		long memberId = 0L;
		
		if(!StringUtils.isTrimEmpty (str))
		{
			memberId = Long.parseLong (str);
		}
		
		int currentPageNum = 1;
		String current_pageStr = request.getParameter ("current_page");
		
		if(!StringUtils.isTrimEmpty (current_pageStr))
		{
			currentPageNum = Integer.parseInt (current_pageStr);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		
		List<BlackFriends>  allBlackFriends= blackFriendsService. queryBlackFriends  (memberId);
		
		int size = allBlackFriends.size ();
		int remainder = size%maxPageRowCount;
		long TotalRowCount = 0L;
		
		if(remainder!=0)
		{
			TotalRowCount = (size/maxPageRowCount)+1;
		}else
		{
			TotalRowCount = size/maxPageRowCount;
		}
		
		List<BlackFriends> newBlackFriends = new ArrayList<BlackFriends>();
		
		if(currentPageNum==TotalRowCount&&ObjectUtils.notEmpty (allBlackFriends))
		{
			for(int i = (int)currentPageNum*maxPageRowCount-maxPageRowCount;i<size;i++)
			{
				newBlackFriends.add (allBlackFriends.get (i));
			}
		}else if(currentPageNum==1&&ObjectUtils.notEmpty (allBlackFriends))
		{
			for(int i = 0;i<maxPageRowCount;i++)
			{
				newBlackFriends.add (allBlackFriends.get (i));
			}
		}
		else if(ObjectUtils.notEmpty (allBlackFriends))
		{
			for(int i = (int)(currentPageNum*maxPageRowCount-maxPageRowCount);i<(currentPageNum*maxPageRowCount-2)+maxPageRowCount;i++)
			{
				newBlackFriends.add (allBlackFriends.get (i));
			}
		}
		
		List<BlackFriendsDTO> dtos = null;
		if(ObjectUtils.notEmpty  (newBlackFriends))
		{
			returnPage = new Page<BlackFriendsDTO> ();
			dtos = new ArrayList<BlackFriendsDTO>();
			List<BlackFriends> list = newBlackFriends;
			list = blackFriendsSortByCreateTime (list);
			for(BlackFriends fp:list)
			{
				long visitor_MId = fp.getFriend_id  ();
				Member m = memberService.getMemberById (visitor_MId);
				if(ObjectUtils.notNull (m))
				{
					BlackFriendsDTO fpdto = new BlackFriendsDTO ();
					fpdto.setId (m.getId ());
					String headUrl = m.getHeadUrl ();
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
					}
					
					fpdto.setHeadUrl (headUrl);
					fpdto.setNickName (m.getNickName ());
					fpdto.setVisitorTime (timeTag (fp.getCreateTime  ()));
					
					dtos.add (fpdto);
				}
			}
			returnPage.setDatas (dtos);
			returnPage.setTotalRowCount (dtos.size ());
			returnPage.setCurrentPageNum (currentPageNum);
			returnPage.setMaxPageRowCount (maxPageRowCount);
			returnPage.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=allFriends");
			returnPage.setTemplate ("pshow");
			pageService.build (returnPage);
		}
		
		return returnPage;
	}
	
	public List<BlackFriends> blackFriendsSortByCreateTime(List<BlackFriends> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getCreateTime   ()<list.get (j).getCreateTime  ())
				{
					BlackFriends temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
}
